package org.rednebula.framework.core.context;

import java.io.Serializable;
import java.util.Enumeration;

import javax.servlet.http.HttpSession;

import org.rednebula.framework.core.BasicContext;
import org.rednebula.framework.core.utils.Struts2Utils;




public class SessionContext extends BasicContext implements Serializable {

	public SessionContext() {
		super();
		this.setName(SESSION);
	}

	public void close() {

	}
	public HttpSession getHttpSession(){
		return Struts2Utils.getSession();
	}
	public <T> T get(String key) {
		if(this.isSelf(key)) return (T)this.getHttpSession();
		return (T)getHttpSession().getAttribute(key);
	}

	public <T> T get(Class<T> type) {
		return (T)getHttpSession().getAttribute(type.getName());
	}

	public boolean has(String name) {
		if(name==null)return false;
		if(isSelf(name))return true;
		HttpSession session=getHttpSession();
		Enumeration<String> ns= session.getAttributeNames();
		while(ns.hasMoreElements()){
			if(name.equals(ns.nextElement()))return true;
		}
		return false;
	}

}
